using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace SubSonic.Security.Data
{
	/// <summary>
	/// Strongly-typed collection for the Role class.
	/// </summary>
	[Serializable]
	public partial class RoleCollection : ActiveList<Role, RoleCollection> 
	{	   
		public RoleCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Roles table.
	/// </summary>
	[Serializable]
	public partial class Role : ActiveRecord<Role>
	{
		#region .ctors and Default Settings
		
		public Role()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Role(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Role(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Role(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Roles", TableType.Table, DataService.GetInstance("SubSonicProviders"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarRoleID = new TableSchema.TableColumn(schema);
				colvarRoleID.ColumnName = "RoleID";
				colvarRoleID.DataType = DbType.Guid;
				colvarRoleID.MaxLength = 0;
				colvarRoleID.AutoIncrement = false;
				colvarRoleID.IsNullable = false;
				colvarRoleID.IsPrimaryKey = true;
				colvarRoleID.IsForeignKey = false;
				colvarRoleID.IsReadOnly = false;
				colvarRoleID.DefaultSetting = @"";
				colvarRoleID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRoleID);
				
				TableSchema.TableColumn colvarRoleName = new TableSchema.TableColumn(schema);
				colvarRoleName.ColumnName = "RoleName";
				colvarRoleName.DataType = DbType.String;
				colvarRoleName.MaxLength = 50;
				colvarRoleName.AutoIncrement = false;
				colvarRoleName.IsNullable = false;
				colvarRoleName.IsPrimaryKey = false;
				colvarRoleName.IsForeignKey = false;
				colvarRoleName.IsReadOnly = false;
				colvarRoleName.DefaultSetting = @"";
				colvarRoleName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRoleName);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["SubSonicProviders"].AddSchema("Roles",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("RoleID")]
		public Guid RoleID 
		{
			get { return GetColumnValue<Guid>("RoleID"); }

			set { SetColumnValue("RoleID", value); }

		}

		  
		[XmlAttribute("RoleName")]
		public string RoleName 
		{
			get { return GetColumnValue<string>("RoleName"); }

			set { SetColumnValue("RoleName", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public SubSonic.Security.Data.UsersInRoleCollection UsersInRoles()
		{
			return new SubSonic.Security.Data.UsersInRoleCollection().Where(UsersInRole.Columns.RoleID, RoleID).Load();
		}

		#endregion
		
			
		
		//no foreign key tables defined (0)
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(Guid varRoleID,string varRoleName)
		{
			Role item = new Role();
			
			item.RoleID = varRoleID;
			
			item.RoleName = varRoleName;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(Guid varRoleID,string varRoleName)
		{
			Role item = new Role();
			
				item.RoleID = varRoleID;
				
				item.RoleName = varRoleName;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string RoleID = @"RoleID";
			 public static string RoleName = @"RoleName";
						
		}

		#endregion
	}

}

